#include "dbc_parser.h"
#include <iostream>
#include <vector>

int main(int argc, char* argv[]) {
    if (argc < 2)
    {
        printf("eg: demo  dbc_file\n");
        return -1;
    }
    try {
        // 创建DBC解析器
        DBC::DBCParser dbc(argv[1]);

        for (const auto & it : dbc)
        {
            printf("name: %s id: %x dlc: %lx from: %s\n",
                it.second.get_name().c_str(), it.second.get_id(), it.second.get_dlc(), it.second.get_from().c_str());
            for (auto& sig : it.second)
            {
                printf("Signal: %s ", sig.get_name().c_str());
                printf("To: ");
                for (auto& to : sig.get_to())
                {
                    std::cout << to << ", ";
                    printf("%s , ", to.c_str());
                }
                printf("Signal: %s\n", sig.get_name().c_str());
                printf("order: %d ", static_cast<int>(sig.get_byte_order()));
                printf("start_bit_: %d ", sig.get_startbit());
                printf("length_: %d ", sig.get_length());
                printf("sign_: %d ", static_cast<int>(sig.get_sign()));
                printf("minimum_: %f ", sig.get_minimum());
                printf("maximum_: %f ", sig.get_maximum());
                printf("factor_: %f ", sig.get_factor());
                printf("offset_: %f ", sig.get_offset());
                printf("unit_: %s ", sig.get_unit().c_str());
                printf("multiplexor_: %d ", static_cast<int>(sig.get_multiplexor()));
                printf("multiplexor_num_: %d ", sig.get_multiplexed_num());
                printf("\n");
            }
        }

        // 解析标准CAN数据 (8字节)
        uint8_t can_data[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};
        uint32_t can_id = 0x94;
        std::vector<double> can_results = dbc.parser_message(can_id, can_data, 8);
        
        std::cout << "Standard CAN results (" << can_results.size() << " signals):" << std::endl;
        for (size_t i = 0; i < can_results.size(); ++i) {
            std::cout << "Signal " << i << ": " << can_results[i] << std::endl;
        }
        
        // 解析CANFD数据 (64字节)
        uint8_t canfd_data[64] = {0};
        // 填充一些测试数据
        for (int i = 0; i < 64; ++i) {
            canfd_data[i] = static_cast<uint8_t>(i);
        }
        std::vector<double> canfd_results = dbc.parser_message(can_id, canfd_data, 64);
        std::cout << "\nCANFD results (" << canfd_results.size() << " signals):" << std::endl;
        for (size_t i = 0; i < canfd_results.size(); ++i) {
            std::cout << "Signal " << i << ": " << canfd_results[i] << std::endl;
        }
        
        // 解析CANFD数据 (32字节)
        uint8_t canfd_32byte[32] = {0};
        for (int i = 0; i < 32; ++i) {
            canfd_32byte[i] = static_cast<uint8_t>(i * 2);
        }
        std::vector<double> canfd_32_results = dbc.parser_message(can_id, canfd_32byte, 32);
        std::cout << "\n32-byte CANFD results (" << canfd_32_results.size() << " signals):" << std::endl;
        for (size_t i = 0; i < canfd_32_results.size(); ++i) {
            std::cout << "Signal " << i << ": " << canfd_32_results[i] << std::endl;
        }


        uint8_t creat_data[8];
        dbc.create_message(148, {8}, creat_data, 8);
        printf("create message:");
        for (unsigned char i : creat_data)
        {
            printf("%x ", i);
        }
        printf("\n");

        // --- Verification Test ---
        std::cout << "\n--- Running Verification Test ---" << std::endl;
        uint32_t test_can_id = 148; // Using ID 148 (0x94)
        std::vector<double> original_values = { 15.5 }; // Example physical value for the signal in message 148

        uint8_t test_data[8];
        bool success_create = dbc.create_message(test_can_id, original_values, test_data, 8);

        if (success_create) {
            std::cout << "Successfully created message for verification." << std::endl;
            printf("Generated data: ");
            for(int i=0; i<8; ++i) printf("0x%02X ", test_data[i]);
            printf("\n");

            std::vector<double> parsed_values = dbc.parser_message(test_can_id, test_data, 8);
            
            if (!parsed_values.empty()) {
                std::cout << "Successfully parsed message for verification." << std::endl;
                std::cout << "Original values: ";
                for(const auto& v : original_values) std::cout << v << " ";
                std::cout << std::endl;

                std::cout << "Parsed values:   ";
                for(const auto& v : parsed_values) std::cout << v << " ";
                std::cout << std::endl;

                bool match = true;
                if (original_values.size() == parsed_values.size()) {
                    for (size_t i = 0; i < original_values.size(); ++i) {
                        if (std::abs(original_values[i] - parsed_values[i]) > 1e-6) {
                            match = false;
                            break;
                        }
                    }
                } else {
                    match = false;
                }

                if (match) {
                    std::cout << "VERIFICATION SUCCESS: Parsed values match original values." << std::endl;
                } else {
                    std::cout << "VERIFICATION FAILED: Parsed values DO NOT match original values." << std::endl;
                }
            } else {
                std::cerr << "Verification Error: Failed to parse the generated message." << std::endl;
            }
        } else {
            std::cerr << "Verification Error: Failed to create message with test values." << std::endl;
        }
        
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}
